package com.bifrost.web.controller;

import com.bifrost.annotation.Authorization;
import com.bifrost.annotation.CurrentStore;
import com.bifrost.annotation.CurrentUser;
import com.bifrost.entity.Customer;
import com.bifrost.entity.MerchantStore;
import com.bifrost.entity.ShoppingCartItem;
import com.bifrost.common.model.ResultModel;
import com.bifrost.model.cart.CartResult;
import com.bifrost.repository.ShoppingCartItemRepository;
import com.bifrost.service.CustomerService;
import com.bifrost.service.ShoppingCartService;
import com.bifrost.service.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author rangf
 * @date 2017/11/6
 */
@Api("购物车接口")
@RestController
@RequestMapping("/api/carts")
public class ShoppingCartController {


    @Autowired
    ShoppingCartService cartService;
    @Autowired
    ShoppingCartItemRepository cartItemRepository;
    @Autowired
    CustomerService customerService;
    @Autowired
    StoreService storeService;
    @Authorization
    @ApiOperation("添加购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "skuId", value = "skuId", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "quantity", value = "商品数量", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "hasService", value = "是否有服务【1有,默认0无】", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "skuServicePriceId", value = "sku服务价格Id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "serviceMerchantId", value = "服务门店Id", required = false, dataType = "Long"),
    })
    @PostMapping("/addToCarts/deprecated")
    public ResultModel addToCart(@CurrentUser Long userId, @RequestParam Long skuId, String hasService, @RequestParam Long quantity,
                                 String skuServicePriceId, Long serviceMerchantId) {
        Customer customer = customerService.findCustomer(userId);
        return cartService.addToCarts(customer, skuId, quantity, hasService, skuServicePriceId, serviceMerchantId);

    }

    @Authorization
    @ApiOperation("添加购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "skuId", value = "skuId", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "quantity", value = "商品数量", required = true, dataType = "Long")
    })
    @PostMapping("/addToCarts")
    public ResultModel addToCartByStore(@CurrentStore Long storeId, @CurrentUser Long userId, @RequestParam Long skuId, @RequestParam Long quantity) {
        if(storeId != null){
            Customer customer = customerService.findCustomer(userId);
            return cartService.addToCartsByStore(customer.getMerchantStore(), customer, skuId, quantity);
        }
        return ResultModel.error("添加失败");
    }

    @Authorization
    @ApiOperation("获取购物车详情")
    @GetMapping("/carts/deprecated")
    public ResultModel getCarts(@CurrentUser Long userId) {
        Customer customer = customerService.findCustomer(userId);
        List<CartResult> carts = cartService.getCarts(customer);
        if (carts != null && !carts.isEmpty()) {
            return ResultModel.ok().put("carts", carts);
        }
        return ResultModel.ok("购物车为空");
    }
    @Authorization
    @ApiOperation("获取购物车详情")
    @GetMapping("/carts")
    public ResultModel getCartsByStore(@CurrentStore Long storeId, @CurrentUser Long userId) {
        List<CartResult> carts = null;
        if(userId != null){
            Customer customer = customerService.findCustomer(userId);
            carts = cartService.getCartsByStore(customer, customer.getMerchantStore());
        }
        return ResultModel.ok().put("carts", carts);
    }

    @Authorization
    @ApiOperation("获取购物车数量")
    @GetMapping("/cartsQuantity/deprecated")
    public ResultModel getCartsQuantity(@CurrentUser Long userId) {
        Customer customer = customerService.findCustomer(userId);
        Long quantity = cartService.getCartsQuantity(customer);
        if(quantity != null && quantity != 0L){
            return ResultModel.ok().put("quantity", quantity);
        }
        return ResultModel.ok().put("quantity", 0);
    }

    @Authorization
    @ApiOperation("获取购物车数量")
    @GetMapping("/cartsQuantity")
    public ResultModel getCartsQuantityByStore(@CurrentStore Long storeId, @CurrentUser Long userId) {
        Long quantity = 0L;
        if(userId != null){
            Customer customer = customerService.findCustomer(userId);
            Long count = cartService.getCartsQuantityByStore(customer.getMerchantStore(), customer);
            if(count != null){
                return ResultModel.ok().put("quantity", count);
            }
        }
        return ResultModel.ok().put("quantity", quantity);
    }

    @Authorization
    @ApiOperation("修改购物车")
    @PostMapping("/cartsUpdate")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cartId", value = "购物车Id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "skuId", value = "skuId", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "quantity", value = "数量", required = true, dataType = "Long")
    })
    public ResultModel cartUpdate(@CurrentStore Long storeId, @CurrentUser Long userId,
                                  @RequestParam Long cartItemId, @RequestParam Long quantity) {
        Customer customer = customerService.findCustomer(userId);
        if(cartService.updateCarts(customer, customer.getMerchantStore(), cartItemId, quantity)){
            ShoppingCartItem item = cartItemRepository.findOne(cartItemId);
            Long qty = null;
            if(item!=null){
                qty = item.getItemQuantity();
            }
            return ResultModel.ok("修改成功").put("quantity",qty==null?0:qty);
        }else {
            ShoppingCartItem item = cartItemRepository.findOne(cartItemId);
            Long qty = null;
            if(item!=null){
                qty = item.getItemQuantity();
            }
            return ResultModel.error("库存不足").put("quantity",qty==null?0:qty);
        }

    }

    @Authorization
    @ApiOperation("移除购物车")
    @PostMapping("/cartsDelete")
    @ApiImplicitParam(paramType = "query", name = "cartItemIds", value = "所勾选itemCartId字符串数组【英文逗号分隔】", required = true, dataType = "String")
    public ResultModel cartsDelete(@CurrentStore Long storeId, @CurrentUser Long userId,
                                   @RequestParam String cartItemIds) {
        String[] ids = cartItemIds.split(",");
        Customer customer = customerService.findCustomer(userId);
        if(cartService.deleteCarts(customer, customer.getMerchantStore(), ids)){
            return ResultModel.ok("移除成功");
        }
        return ResultModel.error("移除失败");
    }

}
